/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import modele.NewHibernateUtil;
import modele.Universite;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;

/**
 *
 * @author Aimee
 */
public class AjoutUniversite extends ActionSupport implements SessionAware {

    private Map<String, Object> session;
    private Session hibernateSession;
    String adresseUniversite, nomUniversite;
    private List<Universite> listeUniversite;
    
    /**
     * @return the session
     */
    public Map<String, Object> getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public String ajoutUniversite() {
        setHibernateSession(NewHibernateUtil.getSessionFactory().openSession());
        getHibernateSession().beginTransaction();
        Universite univ = new Universite();
        univ.setAdresseUniversite(getAdresseUniversite());
        univ.setNomUniversite(getNomUniversite());
        getHibernateSession().save(univ);
        getHibernateSession().getTransaction().commit();
        
        setListeUniversite((List<Universite>) hibernateSession.createQuery("from Universite").list());
        session.put("listeUniversite", getListeUniversite());
        
        return "ok";
    }

    /**
     * @return the adresseUniversite
     */
    public String getAdresseUniversite() {
        return adresseUniversite;
    }

    /**
     * @param adresseUniversite the adresseUniversite to set
     */
    public void setAdresseUniversite(String adresseUniversite) {
        this.adresseUniversite = adresseUniversite;
    }

    /**
     * @return the hibernateSession
     */
    public Session getHibernateSession() {
        return hibernateSession;
    }

    /**
     * @param hibernateSession the hibernateSession to set
     */
    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }

    /**
     * @return the nomUniversite
     */
    public String getNomUniversite() {
        return nomUniversite;
    }

    /**
     * @param nomUniversite the nomUniversite to set
     */
    public void setNomUniversite(String nomUniversite) {
        this.nomUniversite = nomUniversite;
    }

    /**
     * @return the listeUniversite
     */
    public List<Universite> getListeUniversite() {
        return listeUniversite;
    }

    /**
     * @param listeUniversite the listeUniversite to set
     */
    public void setListeUniversite(List<Universite> listeUniversite) {
        this.listeUniversite = listeUniversite;
    }
}
